﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Diagnostics;

namespace WatiNCF.Automater.Business.Adapters
{
    public class PropertyInfoAdapter : InfoAdapter
    {
        public PropertyInfoAdapter(object target, PropertyInfo info)
            : base(target)
        {
            PropertyInfo = info;
            IsProperty = true;
        }

        public PropertyInfo PropertyInfo { get; set; }

        public override string GetName()
        {
            return PropertyInfo.Name;
        }

        public override object GetValue()
        {
            object value = null;
            try
            {
                value = PropertyInfo.GetValue(this.Target, null);
            }
            catch
            {
                Debug.WriteLine(string.Format("Unable to get value for {0}", GetName()));
            }
            return value;
        }

        public override object SetValue(object value)
        {
            try
            {
                PropertyInfo.SetValue(this.Target, value, null); 
            }
            catch
            {
                Debug.WriteLine(string.Format("Unable to set value for {0}", GetName()));
            }

            return value;
        }

        public override Type GetInfoType()
        {
            return PropertyInfo.PropertyType;
        }
    }
}
